package com.sfpay.axg.business.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.web.context.ServletContextAware;

import com.sfpay.axg.common.util.FileUtil;
import com.sfpay.axg.service.integration.webservice.sfexpress.util.SFOrder;

public class XmlParseUtil implements ServletContextAware, InitializingBean {
	/** 临时文件目录，在初始化方法中设置 */
	private static String TMP_DIR = "";
	/** 目录连接符 */
	private static final String DIR_SPLIT_KEY = "/";

	private Marshaller marshaller;
	private Unmarshaller unmarshaller;
	private ServletContext ctx;

	// public void loadSimpleBean() throws IOException {
	// FileInputStream is = null;
	// try {
	// is = new FileInputStream(FILE_NAME);
	// this.simpleBean = (SimpleBean) this.unmarshaller
	// .unmarshal(new StreamSource(is));
	// } finally {
	// if (is != null) {
	// is.close();
	// }
	// }
	// }

	public Object parseXmlStringToBean(String xmlString) throws IOException {

		if (StringUtils.isBlank(xmlString)) {
			return null;
		}
		
		byte[] in = xmlString.getBytes("UTF-8");
		ByteArrayInputStream bti = new ByteArrayInputStream(in);
		Object obj = null;

		try {
			obj = this.unmarshaller.unmarshal(new StreamSource(bti));
		} finally {
			if (bti != null) {
				bti.close();
			}
		}

		return obj;
	}

	/**
	 * SFOrder转换为XML字符串
	 * 
	 * @param sfOrder
	 * @return
	 * @throws IOException
	 */
	public String parseSFOrderToXmlString(SFOrder sfOrder) throws IOException {

		String xmlString = StringUtils.EMPTY;
		FileOutputStream os = null;
		String xmlFile = null;

		try {
			xmlFile = TMP_DIR + DIR_SPLIT_KEY + sfOrder.getOrderId()
					+ "_sforder.xml";
			os = new FileOutputStream(xmlFile);
			this.marshaller.marshal(sfOrder, new StreamResult(os));
			xmlString = FileUtil.readFile2String(xmlFile);
		} finally {
			if (os != null) {
				os.close();
			}
		}
		if (StringUtils.isNotBlank(xmlFile)) {
			FileUtil.deleteFile(xmlFile);
		}
		return xmlString;

	}

	public void setMarshaller(Marshaller marshaller) {
		this.marshaller = marshaller;
	}

	public void setUnmarshaller(Unmarshaller unmarshaller) {
		this.unmarshaller = unmarshaller;
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.ctx = servletContext;
	}

	/**
	 * 设置临时目录
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		if(null == this.ctx){
			TMP_DIR = System.getProperty("user.home");
		}else{
			TMP_DIR = this.ctx.getRealPath("/WEB-INF/tmp");
			File dir = new File(TMP_DIR);
			if (!dir.exists()) {
				dir.mkdir();
			}
		}
	}
}
